<template>
  <div class="container">
    <BaseTable :data="shipSchedule" :columns="shipScheduleTableColumns" :tableHeight="86" :pagination="false"/>
    <BaseTable :data="distributionDetails.tallyBasicInfoList" :columns="tallyBasicInfoTableColumns" :tableHeight="tallyBasicInfoTableHeight"
               :pagination="false" :ShowSummary="tallyBasicInfoTableShowSummary" :getSummariesOut="tallyBasicInfoTableGetSummaries"/>
  </div>
</template>

<script>
import BaseTable from "@/components/BaseTable";

export default {
  name: "index",
  components: {BaseTable},
  data() {
    return {
      shipSchedule: [],
      distributionDetails: {
        shipName: '',
        voyage: '',
        handoverTon: '',
        shipperName: '',
        cargoInfoName: '',
        berthDate: '',
        leaveDate: '',
        tallyBasicInfoList: [],
      },
      shipScheduleTableColumns: [
        {
          label: '船名',
          prop: 'shipName',
        },
        {
          label: '航次',
          prop: 'voyage',
        },
        {
          label: '交接吨',
          prop: 'handoverTon',
        },
        {
          label: '货主',
          prop: 'shipperName',
        },
        {
          label: '货名',
          prop: 'cargoInfoName',
        },
        {
          label: '靠泊日期',
          prop: 'berthDate',
        },
        {
          label: '离港日期',
          prop: 'leaveDate',
        },
      ],
      tallyBasicInfoTableColumns: [
        {
          label: '出港/直取',
          prop: 'inoutPortLabel',
        },
        {
          label: '分运日期',
          prop: 'workDate',
        },
        {
          label: '分运方式',
          prop: 'destinationLabel',
        },
        {
          label: '船名',
          prop: 'bargeName',
        },
        {
          label: '航次',
          prop: 'bvoyage',
        },
        {
          label: '商务委托',
          prop: 'trustNo',
        },
        {
          label: '分运货主',
          prop: 'shipperName',
        },
        {
          label: '分运件数',
          prop: 'pcs',
        },
        {
          label: '分运吨位',
          prop: 'weight',
        },
        {
          label: '库场',
          prop: 'storehouseName',
        },
        {
          label: '货名',
          prop: 'cargoInfoName',
        },
        {
          label: '流向',
          prop: 'flow',
        },
        {
          label: '操作过程',
          prop: 'processName',
        },
        {
          label: '交接方式',
          prop: 'handoverTypeLabel',
        },
      ],
      tallyBasicInfoTableShowSummary: {
        show: true,
        sumText: "合计",
        method: null,
      },
    }
  },
  methods: {
    // 分运明细列表
    listDistributionDetails(shipApplyId = '', shipperId = '') {
      this.$http.get(`/${this.$ServiceName}/api/external/distributiondetails/v1/listdistributiondetails?shipScheduleId=${shipApplyId}&shipperId=${shipperId}`).then(resp => {
        this.distributionDetails = resp.data.data
        this.shipSchedule = [this.distributionDetails]
      })
    },
    // 合计
    tallyBasicInfoTableGetSummaries({columns, data}) {
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        if (column.property === 'pcs' || column.property === 'weight') {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            if (index === 7) {
              sums[index] = sums[index].toFixed(0)
            }
            if (index === 8) {
              sums[index] = sums[index].toFixed(3)
            }
          } else {
            sums[index] = ''
          }
        }
      })
      return sums
    },
  },
  mounted() {
  },
  computed: {
    tallyBasicInfoTableHeight() {
      return this.$store.state.tableHeight - 62
    },
  },
}
</script>